﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Cesave.Domain.Core.Entities;
using Cesave.Domain.PermisosModule.Contracts.CultivosEstaciones;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.CultivosEstaciones
{
	public partial class CultivosEstacionesViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<CultivosEstacion> _cultivosEstaciones;

		/// <summary>
		/// 
		/// </summary>
		private CultivosEstacion _cultivosEstacion;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<CultivosEstacion> CultivosEstaciones
		{
			get
			{
				return _cultivosEstaciones;
			}
			private set
			{
				_cultivosEstaciones = value;
				if (_cultivosEstaciones != null)
					RaisePropertyChanged("CultivosEstaciones");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public CultivosEstacion CultivosEstacion
		{
			get
			{
				return _cultivosEstacion;
			}
			private set
			{
				_cultivosEstacion = value;
				if (_cultivosEstacion != null)
					RaisePropertyChanged("CultivosEstacion");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadCultivosEstaciones()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					ICultivosEstacionService service = IoCFactory.Resolve<ICultivosEstacionService>();
					e.Result = service.FindAllCultivosEstaciones();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						CultivosEstaciones = e.Result as List<CultivosEstacion>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewCultivosEstacion(object sender, EventArgs e)
		{
			_cultivosEstacionesView.cultivosEstacionesBindingSource.AddingNew += AddingNewCultivosEstacion;
			_cultivosEstacionesView.cultivosEstacionesBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewCultivosEstacion(object sender, AddingNewEventArgs e)
		{
			CultivosEstacion = new CultivosEstacion
			{
				Descripcion = string.Empty,
				EstacionGuid = Guid.NewGuid(),
				EstacionId = 0,
				Inicio = DateTime.Now,
				Fin = DateTime.Now.AddMonths(4),
				Nombre = string.Empty,
				Status = true,
			};
			CultivosEstacion.MarkAsAdded();
			e.NewObject = CultivosEstacion;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveCultivosEstaciones(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, args) =>
				{
					ICultivosEstacionService service = IoCFactory.Resolve<ICultivosEstacionService>();
					CultivosEstaciones.ForEach(es =>
					{
						if (es.ChangeTracker.State == ObjectState.Added)
							service.AddCultivosEstacion(es);
						else
							service.ChangeCultivosEstacion(es);
					});
				};
				worker.RunWorkerCompleted += (s, args) =>
				{
					EndWaiting();
					if (args.Error != null)
						ShowMessage(args.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentCultivosEstacion(object sender, EventArgs e)
		{
			BindingSource bindingSoruce = sender as BindingSource;
			if ((bindingSoruce != null) && (bindingSoruce.Current != null))
				CultivosEstacion = bindingSoruce.Current as CultivosEstacion;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnCultivosEstacionesPropertyChanged()
		{
			_cultivosEstacionesView.cultivosEstacionesBindingSource.DataSource = CultivosEstaciones;
			_cultivosEstacionesView.cultivosEstacionesBindingSource.CurrentChanged += SetCurrentCultivosEstacion;
			SetCurrentCultivosEstacion(_cultivosEstacionesView.cultivosEstacionesBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnCultivosEstacionPropertyChanged()
		{
		}

		#endregion

	}
}